<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<style type="text/css">
.lab1{ width:115px; text-align:right; display:inline-block;}
</style>
<div>
<form method="post" action="<?php  echo URL::base() ?>returnedparcel/createdbatch"><input style=" margin-bottom:10px;" type='submit' value='新增一个批次号' name='created_batch' class="btn btn-info"></form></div>

<div style='margin:10px 0;'>
<span class="lab1">请输入批次号：</span>
<input id="select_pi" type="text">
<input class="btn btn-success" type='button' value='下载此批号退货清单' style=" margin-left:10px;" onclick="down_returned()">
</div>

<div>
<span class="lab1">请选择退件原因：</span>
<select id='reason'><option value=1>安检不合格，原单退回</option><option value=2>海外退件，原单退回</option><option value=3>客户退货，已回公司</option></select>
</div>

<div style='margin-top:10px;'>

<span class="lab1">请扫描订单号或追踪码： </span><input style="margin-left:5px;" id='erp_orderid' type='text' > <input style="margin-left:10px;" type="button" class="btn btn-info" id='button_id' value="查询"/>

</div>
<div  style='margin-top:10px'>
<table border=1 cellpadding=10 width=30% id='parcel' class="table" style="width:60%">
<tr style=" background-color:#4EA392; color:#fff;"><th>序号</th><th>erp订单号</th><th>运输方式</th><th>运单号</th></tr>
</table>
</div>

<script>
var num=0;
var pi_ci='';
$().ready(function(){
	var availableTags = [<?php echo $batches;?>];
	$("#select_pi").autocomplete({
       source: availableTags
	});

//1.点击确定按钮进行添加
  $("#button_id").click(function(e){
		$.getJSON("<?php echo URL::site() ?>returnedparcel/insert",{erp_orderid:$("#erp_orderid").val(),reason:$("#reason").val(),pici:$("#select_pi").val()},function(result){
		if(result['did']=='Y'){
		if(pi_ci==''){
			pi_ci=$.trim($("#select_pi").val());
			num=1;
		}else{
			if(pi_ci==$.trim($("#select_pi").val())){
				num++;
			}else{
				$(".rows").remove();
				pi_ci=$.trim($("#select_pi").val());
				num=1;
			}
		}
		var newtable = "";
		if(result['erporderid']==null)result['erporderid']='';
		if(result['traceway']==null)result['traceway']='';
		if(result['tracenumber']==null)result['tracenumber']='';
		newtable +="<tr class='rows'><td>"+num+"</td><td>"+result['erporderid']+"</td><td>"+result['traceway']+"</td><td>"+result['tracenumber']+"</td></tr>";

		newtable +="";
    $("#parcel").append(newtable);
    $("#erp_orderid").val("");

		}else{
		alert(result['reason']);
		}
});
		$("#erp_orderid").focus();

});
//2.按enter键进行添加
 $("#erp_orderid").keydown(function(e){
    if(e.which==13){
		$.getJSON("<?php echo URL::site() ?>returnedparcel/insert",{erp_orderid:$("#erp_orderid").val(),reason:$("#reason").val(),pici:$("#select_pi").val()},function(result){
		if(result['did']=='Y'){
		if(pi_ci==''){
			pi_ci=$.trim($("#select_pi").val());
			num=1;
		}else{
			if(pi_ci==$.trim($("#select_pi").val())){
				num++;
			}else{
				$("#parcel").empty();
				pi_ci=$.trim($("#select_pi").val());
				num=1;
			}
		}
		var newtable = "";
		if(result['erporderid']==null)result['erporderid']='';
		if(result['traceway']==null)result['traceway']='';
		if(result['tracenumber']==null)result['tracenumber']='';
		newtable +="<tr><td>"+num+"</td><td>"+result['erporderid']+"</td><td>"+result['traceway']+"</td><td>"+result['tracenumber']+"</td></tr>";

		newtable +="";
    $("#parcel").append(newtable);
    $("#erp_orderid").val("");

		}else{
		alert(result['reason']);
		}
});
}

});


});
function down_returned(){

	var pici = $("#select_pi").val();
	if(pici==''){
	alert('批次号不能为空，请输入批次号');
	return false;
	}
//	$.ajax({
//		type:'get',
//		url:'<?php echo URL::base();?>returnedparcel/down_returned',
//		dataType:'json',
//		data:{'pici':pici},
//		async:false,
//		success:function(data)
//		{
//			alert("下载成功");
//			window.location.reload; 
//			
//		},
//		error:function()
//		{
//			alert("error");
//		}
//		});
	
 window.location.href="<?php echo URL::site(); ?>returnedparcel/down_returned?pici="+pici;
}
</script>


